using System.Windows.Input;
using CharacterCreation.Views.Contracts;
using Microsoft.Practices.Prism.Commands;
using CharacterCreation.Views.ChildWindows;
using CharacterCreation.Data.Models;
using CharacterCreation.Data;
using CharacterCreation.Data.Services;

namespace CharacterCreation.Views {
    /// <summary>
    /// The character skills view
    /// </summary>
    public partial class CharacterSkillsViewModel : BaseViewModel {

        private CharacterService _characterService = CharacterService.CreateInstance();

        /// <summary>
        /// The currently selected character
        /// </summary>
        public Character Character {
            get { return _characterService.CurrentItem; }
        }

        /// <summary>
        /// The currently selected character skill
        /// </summary>
        public CharacterSkill CharacterSkill {
            get { return _CharacterSkill; }
            set {
                _CharacterSkill = value;
                RaisePropertyChanged("CharacterSkill");
            }
        }
        private CharacterSkill _CharacterSkill; 


        /// <summary>
        /// Edit the current character skill
        /// </summary>
        public ICommand EditSkillCommand {
            get {
                return new DelegateCommand(() => {
                    PopupWindow popupWindow = new PopupWindow(
                       new BaseView[] { new SkillCharacterSkillDetailsView() { CharacterSkill = this.CharacterSkill } }
                    );
                    popupWindow.Closing += (s, e) => {
                        if (popupWindow.DialogResult != null && (bool)popupWindow.DialogResult) {
                            DataContext.Store(this.CharacterSkill);
                        }
                    };
                    popupWindow.ShowDialog();
                });
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CharacterSkillsViewModel() {
            _characterService.PropertyChanged += (s, e) => {
                if (e.PropertyName == "CurrentItem")
                    RaisePropertyChanged("Character");
            };
        }
    }
}